﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace android_charge.compent
{
    public partial class Ip : UserControl
    {
        public Ip()
        {
            InitializeComponent();
            this.BackColor = Color.Transparent; // 背景透明
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            // this.HandleCreated += new EventHandler(OnHandleCreated);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.Parent is Amazon tool)
            {
                tool.changeIp(this);
            }
        }



        public void checkIp()
        {
            if (this.IsHandleCreated && this.Parent is Amazon tool)
            {
                Task.Run(() =>
                {
                    var info_obj = tool.Check_IP();

                    try
                    {
                        this.Invoke(() =>
                        {
                            var oldIp = label2.Text;
                            label2.Text = "";
                            label3.Text = "";
                            if (info_obj != null && info_obj["query"] != null && info_obj["countryCode"] != null)
                            {
                                label2.Text = info_obj["query"].ToString();
                                label3.Text = $"{info_obj["countryCode"].ToString()}({info_obj["regionName"].ToString()})";
                                tool.zip = info_obj["zip"].ToString();
                                if (label2.Text.Equals(oldIp))
                                {
                                    label2.ForeColor = Color.Red;
                                }
                                else
                                {
                                    label2.ForeColor = Color.Blue;
                                }
                            }
                        });
                    }
                    catch
                    {

                    }


                });

            }
        }

        public void OnHandleCreated(object sender, EventArgs e)
        {
            // checkIp();

            //clash_config();
        }

        private void label4_Click(object sender, EventArgs e)
        {
            if (this.Parent is Amazon tool)
            {
                var config = new Amazon_config(tool);
                config.Show();
            }
        }

        private void label1_DoubleClick(object sender, EventArgs e)
        {
            if (this.Parent is Amazon tool)
            {
                tool.listener.StopListening();
                tool.Close();
            }
        }

        private void Ip_Load(object sender, EventArgs e)
        {
            if (this.Parent is Amazon tool)
            {
                tool.clash_config();
                tool.queryLink();
            }
        }
    }
}
